package com.abe.bathsystem.widget.dialog;

import android.content.Context;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.Window;

import com.abe.bathsystem.adapter.AbeCommonAdapter;
import com.abe.bathsystem.adapter.AbeMultiItemTypeAdapter;
import com.abe.bathsystem.adapter.AbeViewHolder;
import com.abe.bathsystem.utils.NormalUtils;
import com.abe.bathsystem.widget.OnOperateListener;
import com.abe.bathsystem.widget.recyclerview.DefaultItemDecoration;
import com.abe.libcore.utils.normal.CommonUtils;
import com.abe.libcore.utils.normal.ToastUtils;

import net.yet.campusdev.R;

import java.util.ArrayList;
import java.util.List;

public class SettingAppsDialog extends BaseDialog {

    public interface OnGotoSettingAppListener {
        void onGoto(ResolveInfo resolveInfo);
    }

    private List<ResolveInfo> mApps;

    public SettingAppsDialog(@NonNull Context context) {
        super(context);
        mApps = NormalUtils.loadApps();
    }

    public SettingAppsDialog(@NonNull Context context, OnOperateListener operateListener) {
        super(context, operateListener);
        mApps = NormalUtils.loadApps();
    }

    public void setListener(OnGotoSettingAppListener listener) {
        this.listener = listener;
    }

    OnGotoSettingAppListener listener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.item_view_user_choose_bathpos);
        initRV();
    }

    private void initRV() {
        RecyclerView itemRv = findViewById(R.id.item_rv);
        if (mApps == null) mApps = NormalUtils.loadApps();
        AbeCommonAdapter<ResolveInfo> adapter = new AbeCommonAdapter<ResolveInfo>(getContext(), R.layout.item_recycler_v3_apps, mApps) {
            @Override
            protected void convert(AbeViewHolder holder, ResolveInfo resolveInfo, int position) {
                holder.setText(R.id.item_menu_name, resolveInfo.activityInfo
                        .loadLabel(getContext().getPackageManager()).toString());
                holder.getView(R.id.item_menu_image).setBackground(resolveInfo.activityInfo.loadIcon(getContext().getPackageManager()));
            }
        };

        adapter.setOnItemClickListener(new AbeMultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (listener != null) {
                    listener.onGoto(mApps.get(position));
                } else {
                    ToastUtils.show("未设置回调监听");
                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        itemRv.setLayoutManager(new GridLayoutManager(getContext(), 4));
        itemRv.addItemDecoration(new DefaultItemDecoration(CommonUtils.getColor(R.color.transparent)));
        itemRv.setAdapter(adapter);
    }
}
